package cn.bluemobi.platform.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.bluemobi.common.core.dto.MapDto;
import cn.bluemobi.common.core.dto.Page;
import cn.bluemobi.common.core.dto.PageCondition;
import cn.bluemobi.platform.core.mybatis.PageUtils;
import cn.bluemobi.platform.entity.order.AfterSales;
import cn.bluemobi.platform.entity.order.AfterSalesItem;
import cn.bluemobi.platform.entity.system.User;
import cn.bluemobi.platform.mapper.AfterSalesMapper;
import cn.bluemobi.platform.service.AfterSalesService;

/**
 * 
 * @author lvxh
 * 
 */
@Service
@Transactional
public class AfterSalesServiceImpl implements AfterSalesService {
    @Autowired
    private AfterSalesMapper afterSalesMapper;

    @Override
    public Page<AfterSales> findAfterSaless(PageCondition cond) {
        Page<AfterSales> page = PageUtils.startPage(cond);
        page.setList(afterSalesMapper.findAfterSaless(cond.getMap()));
        return page;
    }

    @Override
    public AfterSales findAfterSalesDetail(String afterSalesId) {
        AfterSales afterSales = afterSalesMapper.findAfterSalesDetail(afterSalesId);
        if (afterSales != null) {
            List<AfterSalesItem> afterSalesItems = afterSalesMapper.findAfterSalesItems(afterSalesId);
            afterSales.setAfterSalesItems(afterSalesItems);
        }
        return afterSales;
    }

    @Override
    public MapDto deleteAfterSales(String afterSalesId, User user) {
        MapDto dto = new MapDto();
        afterSalesMapper.deleteAfterSales(afterSalesId);
        return dto;
    }

    @Override
    public MapDto auditing(String afterSalesId, String orderId, User user) {
        MapDto dto = new MapDto();
        afterSalesMapper.auditing("1", afterSalesId);
        AfterSalesItem afterSalesItem = afterSalesMapper.findAfterSalesItems(afterSalesId).get(0);
        afterSalesMapper.updateOrderInfoAfterSalesStatus("2", afterSalesItem.getOrderInfoId());

        int orderPassAuditingCount = afterSalesMapper.getOrderPassAuditingCount(orderId);
        int afterSalesPassAuditingCount = afterSalesMapper.getAfterSalesPassAuditingCount(orderId);
        if (orderPassAuditingCount == afterSalesPassAuditingCount) {
            afterSalesMapper.updateOrderAfterSalesFinish(orderId);
        }
        return dto;
    }

    @Override
    public MapDto refuseAuditing(String afterSalesId, String refuseReason, User user) {
        MapDto dto = new MapDto();
        afterSalesMapper.refuseAuditing("2", refuseReason, afterSalesId);
        AfterSalesItem afterSalesItem = afterSalesMapper.findAfterSalesItems(afterSalesId).get(0);
        afterSalesMapper.updateOrderInfoAfterSalesStatus("3", afterSalesItem.getOrderInfoId());
        return dto;
    }

    @Override
    public MapDto confirmPayment(String afterSalesId, User user) {
        MapDto dto = new MapDto();
        afterSalesMapper.confirmPayment("3", afterSalesId);
        AfterSalesItem afterSalesItem = afterSalesMapper.findAfterSalesItems(afterSalesId).get(0);
        afterSalesMapper.updateOrderInfoAfterSalesStatus("4", afterSalesItem.getOrderInfoId());
        return dto;
    }

}
